package com.itheima.consumer.listeners;

import com.itheima.publisher.domain.MultiDelayMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Map;

@Slf4j
@Component
public class MqListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
//    @RabbitListener(queues = "simple.queue")//指定监听那个队列
//    public void listenSimpleQueue(String msg) {
//        System.out.println("消费者接收到的simple.queue的消息：" + msg);
//        throw new RuntimeException("故意抛出异常测试自动返回nack");
//    }
//    @RabbitListener(queues = "work.queue")//指定监听那个队列
//    public void listenWork1Queue(String msg) {
//        System.out.println("消费者1 接收到的simple.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "work.queue")//指定监听那个队列
//    public void listenWork2Queue(String msg) {
//        System.out.println("消费者2 接收到的simple.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "fanout.queue1")//指定监听那个队列
//    public void listenFanout1Queue(String msg) {
//        System.out.println("消费者1 接收到的fanout.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "fanout.queue2")//指定监听那个队列
//    public void listenFanout2Queue(String msg) {
//        System.out.println("消费者2 接收到的fanout.queue的消息：" + msg);
//    }
//    @RabbitListener(
//            bindings = @QueueBinding(//创建联系
//            value = @Queue(value = "direct.queue2", durable = "true"),//联系的队列，可为多个
//            exchange = @Exchange(//联系的交换机
//                    name = "hmall.direct",
//                    type = ExchangeTypes.DIRECT
//            ),
//            key = {"red", "yellow"}
//    ))
//    public void listenDirect1Queue(String msg) {
//        System.out.println("消费者1 接收到的direct.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "direct.queue2")//指定监听那个队列
//    public void listenDirect2Queue(String msg) {
//        System.out.println("消费者2 接收到的direct.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "topic.queue1")//指定监听那个队列
//    public void listenTopic1Queue(String msg) {
//        System.out.println("消费者1 接收到的topic.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "topic.queue2")//指定监听那个队列
//    public void listenTopic2Queue(String msg) {
//        System.out.println("消费者2 接收到的topic.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "object.queue")//指定监听那个队列
//    public void listenObjectQueue(Map<String, Object> msg){
//        System.out.println("消费者2 接收到的object.queue的消息：" + msg);
//    }
//    @RabbitListener(queuesToDeclare = @Queue(
//            name = "lazy.queue",
//            durable = "true",
//            autoDelete = "false",
//            arguments = @Argument(
//                    name = "x-queue-mode",
//                    value = "lazy"
//            )
//    ))
//    public void listenLazyQueue(String msg) {
//        System.out.println("消费者1 接收到的lazy.queue的消息：" + msg);
//    }
//    @RabbitListener(queues = "dlx.queue")//指定监听那个队列
//    public void listenDlxQueue(String msg){
//        System.out.println("消费者2 dlx.queue的消息：" + msg);
//    }
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "delay.queue", durable = "true"),
//            exchange = @Exchange(//创建插件里的延迟交换机
//                    value = "delay.direct",
//                    delayed = "true"
//            ),
//            key = "hi"
//    ))
//    public void listenDelayQueue(String msg){
//        System.out.println("消费者2 delay.queue的消息：" + msg);
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "trade.order.delay.queue", durable = "true"),
            exchange = @Exchange(
                    value = "trade.delay.topic",
                    delayed = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = "order.query"
    ))
    public void listenMultiDelayQueue(MultiDelayMessage<?> msg) {
        System.out.println(msg);
        if (msg.hasNextDelay()) {
            // 创建一个新的消息对象，并复制数据
            Long delay = msg.removeNextDelay();
            // 更新消息对象的延迟时间列表
            if (msg.getDelayMillis().isEmpty()) {
                // 如果延迟时间列表已经为空，则不再发送消息
                return;
            }
            rabbitTemplate.convertAndSend("trade.delay.topic", "order.query", msg, new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setDelay(delay.intValue());
                    return message;
                }
            });
        }
    }

}
